package libs

import (
	"os"
	"path/filepath"
)

type AppIdVersionPair struct {
	AppId   string
	VerName string
}

/**
 * 获得文件目录名称（即App简称）
 */
func AppNames() []string {
	var folders []string

	pwd := `./static`

	//获取文件或目录相关信息
	fileInfoList, err := os.ReadDir(pwd)
	if err != nil {
		return folders
	}

	for _, fileInfo := range fileInfoList {
		if fileInfo.IsDir() {
			name := fileInfo.Name()
			if name != "css" {
				folders = append(folders, name)
			}
		}
	}

	return folders
}

func AppVersion(bundleid string) []AppIdVersionPair {
	var folders []AppIdVersionPair

	pwd := filepath.Join("./static", bundleid)
	//获取文件或目录相关信息
	fileInfoList, err := os.ReadDir(pwd)
	if err != nil {
		return folders
	}

	for _, fileInfo := range fileInfoList {
		if fileInfo.IsDir() {
			one := AppIdVersionPair{
				AppId:   bundleid,
				VerName: fileInfo.Name(),
			}
			folders = append(folders, one)
		}
	}

	return folders
}

// // 限制一天一个医院的文件只能上传5次。防止暴力上传
// func BackupFileCount(pwd string, prefix string) int {
// 	var fileCount int = 0
// 	//获取文件或目录相关信息
// 	fileInfoList, err := ioutil.ReadDir(pwd)
// 	if err != nil {
// 		return -1
// 	}

// 	for _, fileInfo := range fileInfoList {
// 		if !fileInfo.IsDir() {
// 			fileName := fileInfo.Name()
// 			if strings.Contains(fileName, prefix) {
// 				fileCount++
// 			}
// 		}
// 	}

// 	return fileCount
// }
